/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.template.service.impl;


import link.chengguo.orangemall.common.service.impl.BaseServiceImpl;
import link.chengguo.orangemall.common.utils.QueryHelpPlus;
import link.chengguo.orangemall.dozer.service.IGenerator;
import link.chengguo.orangemall.modules.template.domain.OrangeShippingTemplatesRegion;
import link.chengguo.orangemall.modules.template.service.OrangeShippingTemplatesRegionService;
import link.chengguo.orangemall.modules.template.service.dto.OrangeShippingTemplatesRegionDto;
import link.chengguo.orangemall.modules.template.service.dto.OrangeShippingTemplatesRegionQueryCriteria;
import link.chengguo.orangemall.modules.template.service.mapper.OrangeShippingTemplatesRegionMapper;
import link.chengguo.orangemall.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author chengguo
* @date 2020-06-29
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "orangeShippingTemplatesRegion")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OrangeShippingTemplatesRegionServiceImpl extends BaseServiceImpl<OrangeShippingTemplatesRegionMapper, OrangeShippingTemplatesRegion> implements OrangeShippingTemplatesRegionService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(OrangeShippingTemplatesRegionQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<OrangeShippingTemplatesRegion> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), OrangeShippingTemplatesRegionDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<OrangeShippingTemplatesRegion> queryAll(OrangeShippingTemplatesRegionQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(OrangeShippingTemplatesRegion.class, criteria));
    }


    @Override
    public void download(List<OrangeShippingTemplatesRegionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrangeShippingTemplatesRegionDto orangeShippingTemplatesRegion : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("省ID", orangeShippingTemplatesRegion.getProvinceId());
            map.put("模板ID", orangeShippingTemplatesRegion.getTempId());
            map.put("城市ID", orangeShippingTemplatesRegion.getCityId());
            map.put("首件", orangeShippingTemplatesRegion.getFirst());
            map.put("首件运费", orangeShippingTemplatesRegion.getFirstPrice());
            map.put("续件", orangeShippingTemplatesRegion.getContinues());
            map.put("续件运费", orangeShippingTemplatesRegion.getContinuePrice());
            map.put("计费方式", orangeShippingTemplatesRegion.getType());
            map.put("分组唯一值", orangeShippingTemplatesRegion.getUniqid());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
